import 'package:flutter/material.dart';
import 'package:todos_app_core/todos_app_core.dart';
import '../models.dart';
import '../widgets/typedefs.dart';

class AddEditScreen extends StatefulWidget {
  final Todo? todo;
  final TodoAdder addTodo;
  final TodoUpdater updateTodo;
  const AddEditScreen(
      {this.todo, required this.addTodo, required this.updateTodo, Key? key})
      : super(key: key ?? ArchSampleKeys.addTodoScreen);

  @override
  State<AddEditScreen> createState() => _AddEditScreenState();
}

class _AddEditScreenState extends State<AddEditScreen> {
  bool get isEditing => widget.todo != null;
  static final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  String _task = '';
  String _note = '';
  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          title: Text(isEditing
              ? ArchSampleLocalizations.of(context)!.editTodo
              : ArchSampleLocalizations.of(context)!.addTodo),
        ),
        body: Padding(
          padding: const EdgeInsets.all(16),
          child: Form(
            key: formKey,
            autovalidateMode: AutovalidateMode.disabled,
            // onWillPop: () => Future(() => true),
            // onPopInvoked: (didPop) =>
            //     didPop ? Navigator.of(context).pop() : null,
            child: ListView(
              children: [
                TextFormField(
                  initialValue: widget.todo?.task ?? '',
                  key: ArchSampleKeys.taskField,
                  autofocus: !isEditing,
                  decoration: InputDecoration(
                      labelText: ArchSampleLocalizations.of(context)!.addTodo,
                      hintText:
                          ArchSampleLocalizations.of(context)!.newTodoHint),
                  validator: (val) => val!.trim().isEmpty
                      ? ArchSampleLocalizations.of(context)!.emptyTodoError
                      : null,
                  onSaved: (val) => _task = val ?? '',
                ),
                TextFormField(
                  initialValue: widget.todo?.note ?? '',
                  key: ArchSampleKeys.noteField,
                  maxLines: 10,
                  style: Theme.of(context).textTheme.titleMedium,
                  decoration: InputDecoration(
                    labelText: ArchSampleLocalizations.of(context)!.notesHint,
                    hintText: ArchSampleLocalizations.of(context)!.notesHint,
                  ),
                  onSaved: (value) => _note = value ?? '',
                )
              ],
            ),
          ),
        ),
        floatingActionButton: FloatingActionButton(
            key: isEditing
                ? ArchSampleKeys.saveTodoFab
                : ArchSampleKeys.addTodoFab,
            tooltip: isEditing
                ? ArchSampleLocalizations.of(context)!.saveChanges
                : ArchSampleLocalizations.of(context)!.addTodo,
            child: Icon(isEditing ? Icons.check : Icons.add),
            onPressed: () {
              final form = formKey.currentState;
              if (form!.validate()) {
                form.save();
                final task = _task;
                final note = _note;
                if (isEditing) {
                  widget.updateTodo(widget.todo!, task: task, note: note);
                } else {
                  widget.addTodo(Todo(task, note: note));
                }
                Navigator.of(context).pop();
              }
            }),
      );
}
